package ownclasses;


import ij.IJ;

import java.awt.Point;
import java.util.ArrayList;

public class Cluster {
//	Point center;
	float[] values;
	ArrayList<Pixel> pixels;
	
	public Cluster(Point center, float[] values)
	{
//		this.center = center;
		this.values = values;
		pixels = new ArrayList<Pixel>();
	}

	public void remove(Pixel pixel) {
		for(int i = 0; i < pixels.size(); i++)
		{
			if(pixels.get(i).point == pixel.point)
			{
				pixels.remove(i);
				break;
			}
		}
	}

	public double calcDist(float[] other) {
		double dist = Math.sqrt(Math.pow(values[0]-other[0], 2)+
								Math.pow(values[1]-other[1], 2)+
								Math.pow(values[2]-other[2], 2)+
								Math.pow(values[3]-other[3], 2)+
								Math.pow(values[4]-other[4], 2)+
								Math.pow(values[5]-other[5], 2)+
								Math.pow(values[6]-other[6], 2)+
								Math.pow(values[7]-other[7], 2)+
								Math.pow(values[8]-other[8], 2));
		return dist;
	}

	public void add(Pixel newPixel) {
		if(!contains(newPixel))
		{
			//IJ.log("Hinzufügen");
			pixels.add(newPixel);
		}
	}

	private boolean contains(Pixel newPixel) {
		for(Pixel p : pixels)
		{
			if(p.point == newPixel.point)
			{
				IJ.log("Bereits vorhanden in Cluster");
				return true;
			}
		}
		return false;
	}
	
//	public Point getCenter()
//	{
//		return center;
//	}

	public void recomputeCenter() {
		float[] newCenter = new float[9];
		for(Pixel p : pixels)
		{
			for(int i = 0; i < 9; i++)
			{
				newCenter[i] += p.values[i];
			}
		}
		if(pixels.size() != 0)
		{
			for(int i = 0; i < 9; i++)
			{
				 values[i] = newCenter[i] / pixels.size();
			}
			
		}else {
			IJ.log("Cluster hat keine werte");
//			for(int i = 0; i < 9; i++)
//			{
//				 values[i] = 0.0f;
//			}
		}
		
	}
	
	public int getPixels()
	{
		return pixels.size();
	}

}
